// 测试余额计算功能
const testBalanceCalculation = () => {
  console.log('=== 测试余额计算功能 ===')
  
  // 模拟充值记录数据
  const mockRechargeData = [
    {
      orderId: 1,
      amount: 15.00,
      operateTime: '2025-08-08 11:20:00',
      balance: null // 后端没有提供余额
    },
    {
      orderId: 2,
      amount: 40.00,
      operateTime: '2025-08-08 11:10:00',
      balance: null
    },
    {
      orderId: 3,
      amount: 150.00,
      operateTime: '2025-08-08 10:50:00',
      balance: null
    },
    {
      orderId: 4,
      amount: 60.00,
      operateTime: '2025-08-08 10:00:00',
      balance: null
    },
    {
      orderId: 5,
      amount: 320.00,
      operateTime: '2024-08-01 16:05:00',
      balance: null
    }
  ]
  
  console.log('模拟充值记录:', mockRechargeData)
  
  // 模拟当前余额
  const currentBalance = 150.50
  console.log('当前余额:', currentBalance)
  
  // 模拟calculateBalances函数
  const calculateBalances = (dataList, cardBalance) => {
    // 按时间排序（从新到旧）
    const sortedData = [...dataList].sort((a, b) => {
      const timeA = new Date(a.operateTime || a.createTime || a.time || 0)
      const timeB = new Date(b.operateTime || b.createTime || b.time || 0)
      return timeB - timeA
    })
    
    console.log('排序后的数据:', sortedData.map(item => ({
      orderId: item.orderId,
      amount: item.amount,
      operateTime: item.operateTime
    })))
    
    // 从当前余额开始，按时间倒序计算每条记录后的余额
    let runningBalance = parseFloat(cardBalance) || 0
    const balanceMap = new Map()
    
    console.log('开始计算余额...')
    
    // 为每条记录计算余额
    sortedData.forEach((item, index) => {
      const itemId = item.orderId || item.id
      const amount = parseFloat(item.amount) || 0
      
      console.log(`\n处理记录 ${index + 1}:`)
      console.log(`- 订单ID: ${itemId}`)
      console.log(`- 充值金额: ${amount}`)
      console.log(`- 当前运行余额: ${runningBalance}`)
      
      // 如果后端提供了该记录的余额，使用后端数据
      if (item.balance !== undefined && item.balance !== null) {
        balanceMap.set(itemId, parseFloat(item.balance).toFixed(2))
        runningBalance = parseFloat(item.balance)
        console.log(`- 使用后端余额: ${item.balance}`)
      } else {
        // 否则从当前余额减去充值金额
        runningBalance = Math.max(0, runningBalance - amount)
        balanceMap.set(itemId, runningBalance.toFixed(2))
        console.log(`- 计算后余额: ${runningBalance.toFixed(2)}`)
      }
    })
    
    console.log('\n余额映射表:', Object.fromEntries(balanceMap))
    
    // 处理数据并添加计算出的余额
    return dataList.map(item => {
      const itemId = item.orderId || item.id
      const calculatedBalance = balanceMap.get(itemId) || '0.00'
      
      return {
        id: itemId,
        amount: item.amount > 0 ? `+${item.amount.toFixed(2)}` : `${item.amount.toFixed(2)}`,
        balance: calculatedBalance,
        operateTime: item.operateTime
      }
    })
  }
  
  // 执行测试
  const result = calculateBalances(mockRechargeData, currentBalance)
  
  console.log('\n=== 计算结果 ===')
  result.forEach((item, index) => {
    console.log(`记录 ${index + 1}: 充值${item.amount}, 余额${item.balance}`)
  })
  
  // 验证结果
  console.log('\n=== 验证结果 ===')
  const expectedBalances = [
    '135.50', // 150.50 - 15.00
    '95.50',  // 135.50 - 40.00
    '-54.50', // 95.50 - 150.00 (但会被限制为0)
    '-114.50', // -54.50 - 60.00 (但会被限制为0)
    '-434.50'  // -114.50 - 320.00 (但会被限制为0)
  ]
  
  result.forEach((item, index) => {
    const expected = expectedBalances[index]
    const actual = item.balance
    const passed = actual === expected || (parseFloat(actual) === 0 && parseFloat(expected) < 0)
    console.log(`记录 ${index + 1}: 期望${expected}, 实际${actual} ${passed ? '✅' : '❌'}`)
  })
}

// 运行测试
testBalanceCalculation() 